package com.weibo.basic.alg.alert.server.entity;

import com.google.common.base.Strings;
import java.util.List;
import java.util.Map;
import lombok.Data;

@Data
public class Alert {

    List<EvalMatch> evalMatches;
    private String title;
    private Integer ruleId;
    private String ruleName;
    private String ruleUrl;
    private String state;
    private String imageUrl;
    private String message;

    /**
     * alert msg is Alert(evalMatches=[Alert.EvalMatch(metric=redis.10.85.125.2:6379.used_memory, tags=null,
     * value=2.2127817264E9)],
     *
     * title=[Alerting] 使用内存 alert, ruleId=2, ruleName=使用内存 alert, ruleUrl=http://localhost:3000/d/AtOBVAQik/redisjian-kong?fullscreen=true&edit=true&tab=alert&panelId=2&orgId=1,
     * state=alerting, imageUrl=null, message=null)
     */
    public String resolveAlertMsg() {

        StringBuilder stringBuilder = new StringBuilder();
        if (!Strings.isNullOrEmpty(this.ruleName)) {
            stringBuilder.append("异常: " + this.ruleName + "\n");
        }
        if (null != this.getEvalMatches()) {
            for (EvalMatch each : this.getEvalMatches()) {
                stringBuilder.append("异常指标: " + each.getMetric() + ", 当前值:" + each.getValue()).append("\n");
            }
        }
        if (!Strings.isNullOrEmpty(this.ruleUrl)) {
            stringBuilder.append("点击查看详情: " + this.ruleUrl).append("\n");
        }
        if (!Strings.isNullOrEmpty(this.getMessage())) {
            stringBuilder.append("提示: " + this.getMessage()).append("\n");
        }
        return stringBuilder.toString();
    }

    @Data
    public static class EvalMatch {

        private String metric;
        private Map<String, String> tags;
        private Double value;

        public EvalMatch(String metric, Double value) {
            this.metric = metric;
            this.value = value;
        }

        public EvalMatch() {
        }
    }
}
